﻿using Microsoft.UI.Xaml.Data;

namespace DimensionsHelper.Client.WinUi.Helpers;

public partial class CollectionBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        bool result = false;

        switch (value)
        {
            case IList list:
                result = list.Count > 0;
                break;

            case ICollection collection:
                result = collection.Count > 0;
                break;

            case IEnumerable enumerable:
                result = enumerable.GetEnumerator().MoveNext();
                break;
        }

        if (parameter is true || (bool.TryParse(parameter.ToString(), out bool parsed) && parsed))
        {
            return !result;
        }

        return result;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language) =>
        throw new NotImplementedException();
}